package com.surveillance.controllers;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.surveillance.BO.ExamenBO;
import com.surveillance.BO.SurveillantBO;
import com.surveillance.entities.AffectedSurveillants;
import com.surveillance.entities.Examen;
import com.surveillance.entities.Salle;
import com.surveillance.entities.Surveillant;
import com.surveillance.util.ConfigNumbers;
import com.surveillance.util.NiveauSpecialiteBundle;
import com.surveillance.util.SalleNbEnsNbRes;
import com.surveillance.util.SurveillantIds;

@Controller
@SessionAttributes({ "niveauSpecialiteBundle", "fetchedSalles" })
public class affectationSurveillantsController {
	@Autowired
	SurveillantBO surveillantBO;
	@Autowired
	ExamenBO examenBo;

	public void setSurveillantBO(SurveillantBO surveillantBO) {
		this.surveillantBO = surveillantBO;
	}

	public SurveillantBO getSurveillantBO() {
		return surveillantBO;
	}

	public ExamenBO getExamenBo() {
		return examenBo;
	}

	public void setExamenBo(ExamenBO examenBo) {
		this.examenBo = examenBo;
	}

	@RequestMapping(value = "/affectationSurveillants", method = RequestMethod.GET)
	public String renderAffectationView(ModelMap model) {
		NiveauSpecialiteBundle niveauSpecialiteBundle = new NiveauSpecialiteBundle();
		model.addAttribute("niveauSpecialiteBundle", niveauSpecialiteBundle);
		return "affectationStep1";
	}

	@RequestMapping(value = "/affectationSurveillants", method = RequestMethod.POST)
	public String loadAffectationStep2(
			@ModelAttribute("niveauSpecialiteBundle") NiveauSpecialiteBundle niveauSpecialiteBundle,
			ModelMap model) {
		model.addAttribute("niveauSpecialiteBundle", niveauSpecialiteBundle);
		return "redirect:/affectationSurveillantStep2";
	}

	@RequestMapping(value = "/affectationSurveillantStep2", method = RequestMethod.GET)
	public String renderAffectationStep2(ModelMap model, HttpSession session) {
		NiveauSpecialiteBundle niveauSpecialiteBundle = (NiveauSpecialiteBundle) session
				.getAttribute("niveauSpecialiteBundle");
		List<Salle> fetchedSalles = surveillantBO.getSallesToAffect(
				Integer.parseInt(niveauSpecialiteBundle.getNiveau()),
				Boolean.parseBoolean(niveauSpecialiteBundle.getSpecialite()));

		// ajoutons l'ensemble des salles au session
		session.setAttribute("fetchedSalles", fetchedSalles);

		ArrayList<SalleNbEnsNbRes> salleNbEndNbRes = fromSalleArray2SalleNbEnsNbResarray(fetchedSalles);
		ArrayList<Surveillant> fetchedSurveillants = (ArrayList<Surveillant>) surveillantBO
				.findAll();
		// pour recolter les id des surveillants
		SurveillantIds surveillantIds = new SurveillantIds();
		model.addAttribute("fetchedSurveillants", fetchedSurveillants);
		model.addAttribute("availableSalles", salleNbEndNbRes);
		model.addAttribute("niveauSpecialiteBundle", niveauSpecialiteBundle);
		model.addAttribute("surveillantIds", surveillantIds);
		return "affectationSurveillant2";
	}

	@RequestMapping(value = "/affectationSurveillantStep2", method = RequestMethod.POST)
	public String triggerAffectationSurveillants(
			@ModelAttribute("surveillantIds") SurveillantIds surveillantIds,
			HttpSession session, ModelMap model) {

		NiveauSpecialiteBundle niveauSpecialiteBundle = (NiveauSpecialiteBundle) session
				.getAttribute("niveauSpecialiteBundle");

		ArrayList<Surveillant> fetchedSurveillants = (ArrayList<Surveillant>) surveillantBO
				.getSurveillantByIds(surveillantIds.getSurveillantId());

		Examen fetchedExamen = examenBo.findByID(Long
				.parseLong(niveauSpecialiteBundle.getExamen()));

		ArrayList<AffectedSurveillants> affectSurveillantsArray = surveillantBO
				.affectSurveillants(fetchedSurveillants, fetchedExamen, Integer
						.parseInt(niveauSpecialiteBundle.getNiveau()), Boolean
						.parseBoolean(niveauSpecialiteBundle.getSpecialite()));
		System.out.println("affchage de affectSurveillantsArray avant de retourner la page JSP");
		for(int i=0;i<affectSurveillantsArray.size();i++){
			System.out.println(affectSurveillantsArray.get(i));
		}
		model.addAttribute("affectSurveillantsArray", affectSurveillantsArray);
		return "affectationSurveillant3";
	}

	private SalleNbEnsNbRes stickNbEnsNbRes(Salle salle) {
		SalleNbEnsNbRes res = new SalleNbEnsNbRes();
		res.setDesignation(salle.getDesignation());
		// cas d'une salle
		if (salle.getType().equalsIgnoreCase("salle")) {
			res.setNbEns(ConfigNumbers.getNbrEnspersalle());
			res.setNbRes((int) (salle.getCapaciteE() / ConfigNumbers
					.getNbetud1res()));
			// cas d'un amphie
		} else {
			res.setNbEns(ConfigNumbers.getNbrensperamphie());
			res.setNbRes(ConfigNumbers.getNbrresperamphie());
		}
		return res;
	}

	private ArrayList<SalleNbEnsNbRes> fromSalleArray2SalleNbEnsNbResarray(
			List<Salle> salles) {
		ArrayList<SalleNbEnsNbRes> res = new ArrayList<SalleNbEnsNbRes>();
		for (int i = 0; i < salles.size(); i++) {
			res.add(stickNbEnsNbRes(salles.get(i)));
		}
		return res;

	}

}
